//
//  PALoginView.m
//  PAMobileWallet
//
//  Created by Pay_SMP002 on 13-7-3.
//  Copyright (c) 2013年 平安付. All rights reserved.
//

#import "PALoginView.h"
#import "PALoginViewController.h"
#import "PAChekText.h"
#import "UIColor+Extend.h"

@implementation PALoginView
@synthesize loginButton;
@synthesize registerButton;
@synthesize userTF;
@synthesize userBackImage;
@synthesize passTF;
@synthesize passBackImage;
@synthesize userButton;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self){
        // Initialization code

        CGSize size = frame.size;
        CGSize buttonSize = CGSizeMake(578/2, 98/2);
        CGSize tableSize = CGSizeMake(580/2, 100/2);

        UIImageView * loginBackImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 90)];
        loginBackImage.image = [UIImage imageNamed:@"login_banner.png"];
        loginBackImage.backgroundColor = [UIColor clearColor];
        [self addSubview:loginBackImage];

        userBackImage = [[UIImageView alloc] initWithFrame:CGRectMake((size.width-tableSize.width)/2, 90+4, tableSize.width, tableSize.height)];
        userBackImage.image = [UIImage imageNamed:@"login_account.png"];
        userBackImage.backgroundColor = [UIColor clearColor];
        [self addSubview:userBackImage];
        
        UIImageView *accountIconIV = [[UIImageView alloc] initWithFrame:CGRectMake(20, (100-43)/4, 24, 43/2)];
        accountIconIV.image = [UIImage imageNamed:@"login_icon_account.png"];
        [userBackImage addSubview:accountIconIV];
        
        self.userTF = [[UITextField alloc] initWithFrame:CGRectMake(86, 90+4, tableSize.width-40-40-16-15, tableSize.height)];
        self.userTF.keyboardType = UIKeyboardTypeASCIICapable;
        self.userTF.returnKeyType = UIReturnKeyNext;
         [self.userTF setBorderStyle:UITextBorderStyleNone]; //外框类型
        self.userTF.placeholder = @"Email或手机号"; //默认显示的字
#if TARGET_IPHONE_SIMULATOR
        self.userTF.text = @"13764947310";
#endif
        
        self.userTF.secureTextEntry = NO; //密码
        self.userTF.contentHorizontalAlignment = NSTextAlignmentCenter;
        self.userTF.contentVerticalAlignment = NSTextAlignmentLeft;
        self.userTF.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        [self addSubview:self.userTF];
        [self.userTF addTarget:self action:@selector(changeCheckTFState:) forControlEvents:UIControlEventEditingChanged];

        userButton = [UIButton buttonWithType:UIButtonTypeCustom];
        userButton.frame = CGRectMake(tableSize.width - 16, (tableSize.height - (21/2))/2 + 90 +4, 32/2, 21/2);
        userButton.backgroundColor = [UIColor clearColor];
        userButton.tag = 3;
        userButton.selected = NO;
        [userButton setBackgroundImage:[UIImage imageNamed:@"login_userBTdown.png"] forState:UIControlStateNormal];
        [userButton addTarget:self action:@selector(buttonActive:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:userButton];
        
        passBackImage = [[UIImageView alloc] initWithFrame:CGRectMake((size.width-tableSize.width)/2, 90+4+50, tableSize.width, tableSize.height)];
        passBackImage.image = [UIImage imageNamed:@"login_password.png"];
        passBackImage.backgroundColor = [UIColor clearColor];
        [self addSubview:passBackImage];
        
        UIImageView *passIconIV = [[UIImageView alloc] initWithFrame:CGRectMake(20, 13, 43/2, 48/2)];
        passIconIV.image = [UIImage imageNamed:@"login_icon_password.png"];
        [passBackImage addSubview:passIconIV];

        self.passTF = [[UITextField alloc] initWithFrame:CGRectMake(86, 90+4+50, tableSize.width-40-40-16, tableSize.height)];
        self.passTF.keyboardType = UIKeyboardTypeASCIICapable;
        self.passTF.returnKeyType = UIReturnKeyDone;
        [self.passTF setBorderStyle:UITextBorderStyleNone]; //外框类型
        self.passTF.placeholder = @"请输入密码"; //默认显示的字
#if TARGET_IPHONE_SIMULATOR
        self.passTF.text = @"123456";
#endif
        self.passTF.secureTextEntry = YES; //密码
        self.passTF.contentHorizontalAlignment = NSTextAlignmentCenter;
        self.passTF.contentVerticalAlignment = NSTextAlignmentLeft;
        self.passTF.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        [self addSubview:self.passTF];

        //登录按钮
        loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        loginButton.frame = CGRectMake((size.width-buttonSize.width)/2, HEIGHT_LOGINIMAGE + 15 + 100 +20, buttonSize.width, buttonSize.height);
        loginButton.backgroundColor = [UIColor clearColor];
        [loginButton addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
        [loginButton setBackgroundImage:[UIImage imageNamed:@"button_A1.png"] forState:UIControlStateNormal];
        [loginButton setBackgroundImage:[UIImage imageNamed:@"button_A2.png"] forState:UIControlStateHighlighted];
        [loginButton setTitle:@"安全登录" forState:UIControlStateNormal];
        [loginButton setTitleColor:ABLE_COLOR forState:UIControlStateNormal];
        [loginButton setTitleColor:ABLE_COLOR forState:UIControlStateHighlighted];
        loginButton.titleLabel.font = [UIFont systemFontOfSize:18.0];
        loginButton.tag = 0;
        [self addSubview:loginButton];
        
        //注册按钮
        registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        registerButton.frame = CGRectMake((size.width-buttonSize.width)/2, HEIGHT_LOGINIMAGE + 15 + 100 +20 + buttonSize.height + 12, buttonSize.width, buttonSize.height);
        registerButton.backgroundColor = [UIColor clearColor];
        [registerButton addTarget:self action:@selector(registerAction:) forControlEvents:UIControlEventTouchUpInside];
        [registerButton setBackgroundImage:[UIImage imageNamed:@"button_B1.png"] forState:UIControlStateNormal];
        [registerButton setBackgroundImage:[UIImage imageNamed:@"button_B2.png"] forState:UIControlStateHighlighted];
        [registerButton setTitle:@"立刻用手机号注册" forState:UIControlStateNormal];
        [registerButton setTitleColor:ENABLE_COLOR forState:UIControlStateNormal];
        [registerButton setTitleColor:ENABLE_COLOR forState:UIControlStateHighlighted];
        registerButton.titleLabel.font = [UIFont systemFontOfSize:18.0];
        registerButton.tag = 1;
        [self addSubview:registerButton];
    }
    return self;
}
- (void)changeCheckTFState:(UITextField *)getUserNameTF
{
    if ([PAChekText validMobileNo:self.userTF.text]<0 || [PAChekText validEmail:self.userTF.text]<0) {
        self.passTF.text = @"";
    }
}
- (void)loginAction:(id)sender
{
    if (self.parentController && [self.parentController respondsToSelector:@selector(loginAction:)]) {
        if (userTF.text && passTF.text && ![userTF.text isEqualToString:@""] && ![passTF.text isEqualToString:@""]) {
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:userTF.text, k_loginId,passTF.text,k_loginPassword,nil];
            [self.parentController performSelector:@selector(loginAction:) withObject:dic];
        }
    }
}
- (void)registerAction:(id)sender
{
    if (self.parentController && [self.parentController respondsToSelector:@selector(registerAction:)]) {
        [self.parentController performSelector:@selector(registerAction:) withObject:sender];
    }
}
-(void)buttonActive:(id)sender
{
    [self.parentController testNext:sender];
}



@end
